package pl.edu.agh.persdoc.model;

import java.util.*;

import org.ektorp.*;
import org.ektorp.ViewResult.Row;
import org.ektorp.support.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

@Component
public class ExaminationRepository extends CouchDbRepositorySupport<Examination> {

	@Autowired
	public ExaminationRepository(@Qualifier("blogPostDatabase") CouchDbConnector db) {
		super(Examination.class, db);
		initStandardDesignDocument();
	}
	


    @View( name="allPatients", map = "function(doc) { if(doc.patientID){emit([doc.patientID], null)};}",reduce="function(keys, values){return null;}")
    public List<String> getAllPatients() {
            ViewQuery q = createQuery("allPatients").group(true);
            ViewResult queryResult=db.queryView(q);
            List<String> patients=new ArrayList<String>();
            for(Row r:queryResult.getRows()){
            	patients.add(r.getKey().substring(2,r.getKey().length()-2));    	
            }
            System.out.println("patients"+patients.size());
            return patients;
    }
    @GenerateView
    public List<Examination> findByPatientID(String patientID) {
        return queryView("by_patientID", patientID);
    }

}
